package com.example.testproject.activity

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.example.testproject.R
import com.example.testproject.adapter.TopicAdapter
import com.example.testproject.adapter.TopicSubmitAdapter
import com.example.testproject.bean.TopicDetailsBean
import com.example.testproject.bean.TopicSecondBean
import com.example.testproject.view_model.TopicSubmitViewModel
import kotlinx.android.synthetic.main.recylcer_view.*

/**
 *
 * @ClassName: TopicSubmitActivity
 * @Author: android 超级兵
 * @CreateDate: 10/28/21$ 11:05 AM$
 * TODO recyclerView 进阶2 提交页面
 */
class TopicSubmitActivity : AppCompatActivity() {
    companion object {
        const val DATA_SERIALIZABLE = "data"
    }

    val viewModel: TopicSubmitViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(null)
        setContentView(R.layout.recylcer_view)
        val serializableExtra =
            intent.getSerializableExtra(DATA_SERIALIZABLE) as ArrayList<MultiItemEntity>
        Log.i("szj接收到数据拉", serializableExtra.toString())

        val tempList = ArrayList<TopicDetailsBean>()

        // 存储单选
        val type1List = ArrayList<TopicSecondBean>()

        // 存储多选
        val type2List = ArrayList<TopicSecondBean>()

        // 存储判断
        val type3List = ArrayList<TopicSecondBean>()

        // 存储填空
        val type4List = ArrayList<TopicSecondBean>()

        serializableExtra.filterIsInstance<TopicSecondBean>().map {
            when (it.type) {
                0 -> {
                    type1List.add(it)
                }
                1 -> {
                    type2List.add(it)
                }
                2 -> {
                    type3List.add(it)
                }
                3 -> {
                    type4List.add(it)
                }
                else -> ""
            }
        }

        // 单选
        tempList.add(
            TopicDetailsBean(
                TopicAdapter.SINGLE_CHOICE,
                type1List
            )
        )
        // 多选
        tempList.add(
            TopicDetailsBean(
                TopicAdapter.MULTIPLE_CHOICE,
                type2List
            )
        )
        // 判断
        tempList.add(
            TopicDetailsBean(
                TopicAdapter.JUDGE,
                type3List
            )
        )
        // 填空
        tempList.add(
            TopicDetailsBean(
                TopicAdapter.FILL,
                type4List
            )
        )


        val topicSubmitAdapter = TopicSubmitAdapter(tempList)

        rootRecyclerView.layoutManager = LinearLayoutManager(this)

        rootRecyclerView.adapter = topicSubmitAdapter

        topicSubmitAdapter.click = object : TopicSubmitAdapter.OnTopicSubmitListener {
            override fun clickCallBack(type: Int, topicSecondBean: TopicSecondBean) {
                viewModel.showDialog(
                    this@TopicSubmitActivity,
                    supportFragmentManager,
                    topicSecondBean,
                    topicSubmitAdapter,
                    type
                )
            }
        }
    }
}